/**
 * 
 */
package GUILayer;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import ControlLayer.SupplierOrderCtr;
import ModelLayer.Product;
import ModelLayer.Supplier;
import ModelLayer.SupplierOrder;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.Font;

/**
 * @author Frank Eskelund
 * @version 
 */
public class SupplierUI extends JFrame {
	private JTable table;
	private SupplierOrderCtr supplierOrderCtr;
	private ArrayList<Product> productsToOrder;
	private JLabel lblName;
	private JLabel lblEmail;
	private JLabel lblPhone;
	private JLabel lblAddress;
	private JTable tblSupplierOrders;
	private Product activeProduct;
	private JButton btnNewOrder;
	
	public SupplierUI() {
		supplierOrderCtr = new SupplierOrderCtr();
		productsToOrder = supplierOrderCtr.generateSupplierOrder();
		
		getContentPane().setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 11, 964, 188);
		getContentPane().add(scrollPane);
		
		table = new JTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.getTableHeader().setReorderingAllowed(false);
		table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){

			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				int index = table.getSelectedRow();
				if(index > -1){
					btnNewOrder.setEnabled(true);
					int productNumber = Integer.parseInt(table.getValueAt(index, 3).toString());
					Product product = findProduct(productNumber);
					activeProduct = product;
					Supplier supplier = product.getSupplier();
					lblName.setText(supplier.getCo());
					lblEmail.setText(supplier.getEmail());
					lblPhone.setText(supplier.getPhoneNumber());
					lblAddress.setText(supplier.getAddress());
					
					databindOrdersTable();
				}else{
					btnNewOrder.setEnabled(false);
				}
			}
			
		});
		databindTable();
		scrollPane.setViewportView(table);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 210, 250, 241);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		lblName = new JLabel("");
		lblName.setBounds(72, 38, 168, 14);
		panel.add(lblName);
		
		lblEmail = new JLabel("");
		lblEmail.setBounds(72, 58, 168, 14);
		panel.add(lblEmail);
		
		lblPhone = new JLabel("");
		lblPhone.setBounds(72, 106, 168, 12);
		panel.add(lblPhone);
		
		lblAddress = new JLabel("");
		lblAddress.setBounds(72, 83, 168, 12);
		panel.add(lblAddress);
		
		JLabel lblLeverandroplysninger = new JLabel("Leverand\u00F8roplysninger");
		lblLeverandroplysninger.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblLeverandroplysninger.setBounds(10, 11, 190, 14);
		panel.add(lblLeverandroplysninger);
		
		btnNewOrder = new JButton("Nyt indk\u00F8b");
		btnNewOrder.setEnabled(false);
		btnNewOrder.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				final NewSupplierOrderUI newSupplierOrderUI = new NewSupplierOrderUI(supplierOrderCtr, activeProduct);
				newSupplierOrderUI.addWindowListener(new WindowListener(){

					@Override
					public void windowActivated(WindowEvent arg0) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void windowClosed(WindowEvent arg0) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void windowClosing(WindowEvent arg0) {
						lblName.setText("");
						lblEmail.setText("");
						lblPhone.setText("");
						lblAddress.setText("");
						databindTable();
					}

					@Override
					public void windowDeactivated(WindowEvent arg0) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void windowDeiconified(WindowEvent arg0) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void windowIconified(WindowEvent arg0) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void windowOpened(WindowEvent e) {
						// TODO Auto-generated method stub
						
					}


					
				});
			}
		});
		btnNewOrder.setBounds(98, 207, 142, 23);
		panel.add(btnNewOrder);
		
		JLabel lblName_1 = new JLabel("Navn");
		lblName_1.setBounds(10, 38, 46, 14);
		panel.add(lblName_1);
		
		JLabel lblEmail_1 = new JLabel("Email");
		lblEmail_1.setBounds(10, 58, 46, 14);
		panel.add(lblEmail_1);
		
		JLabel lblTelefonnummer = new JLabel("Tlf.");
		lblTelefonnummer.setBounds(10, 104, 74, 14);
		panel.add(lblTelefonnummer);
		
		JLabel lblAdresse = new JLabel("Adresse");
		lblAdresse.setBounds(10, 81, 74, 14);
		panel.add(lblAdresse);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(269, 210, 705, 241);
		getContentPane().add(scrollPane_1);
		
		tblSupplierOrders = new JTable();

		scrollPane_1.setViewportView(tblSupplierOrders);
		this.setSize(1000, 500);
		this.setVisible(true);
	}
	
	private void databindTable()
	{
		Object[][] data = new Object[productsToOrder.size()][4];
		
		for(int i = 0; i < productsToOrder.size(); i++){
			Product product = productsToOrder.get(i);
			data[i] = new Object[]{product.getName(), product.getAmountInStock(), product.getSupplier().getCo(), product.getProductNumber()};
		}
		
		
		table.setModel(new DefaultTableModel(
			data,
			new String[] {
				"Produktnavn", "Antal p\u00E5 lager", "Leverand\u00F8r", "Varenummer"
			}
		));
		
		if(activeProduct != null){
			databindOrdersTable();
		}
		
	}
	
	private void databindOrdersTable(){
		ArrayList<SupplierOrder> orders = activeProduct.getSupplierOrders();
		Object[][] ordersModel = new Object[orders.size()][5];
		
		for(int i = 0; i < orders.size(); i++){
			SupplierOrder supplierOrder = orders.get(i);
			
			DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
			
			String datePlaced = df.format(supplierOrder.getDatePlaced());
			String estimatedInStock = df.format(supplierOrder.getEstimatedInStock());
			
			ordersModel[i] = new Object[]{ datePlaced, estimatedInStock, 
					supplierOrder.getDateDelivered(), supplierOrder.getAmount(), supplierOrder.getStatus().toString()};
		}
		
		tblSupplierOrders.setModel(new DefaultTableModel(
				ordersModel,
				new String[] {
					"Ordredato", "Forventet leveringsdato", "Leveringsdato", "Antal", "Status"
				}
		));
	}
	
	private Product findProduct(int productNo)
	{
		int i = 0;
		boolean found = false;
		Product productToReturn = null;
		while(!found && i < productsToOrder.size())
		{
			Product product = productsToOrder.get(i);
			if(product.getProductNumber() == productNo){
				found = true;
				productToReturn = product;
			}
			
			i++;
		}
		
		return productToReturn;
	}
}
